package com.beep.erp.model.transfer;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.TransferLogBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultFooter;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.common.widget.tips.TipsHelper;
import com.beep.erp.model.transfer.pallet.PalletGoodsActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: TransferActivity
 * description: 扫描
 * Author: Jolly
 * Date: 2018/9/3
 */
public class TransferActivity extends ToolbarActivity<TransferLogListPresenter, TransferLogListModel> implements TransferLogListContract.View, RefreshView.OnFreshListener {

    @BindView(R.id.iv_scan)
    ImageView ivScan;
    @BindView(R.id.appbatlayout)
    AppBarLayout appbatlayout;
    @BindView(R.id.rl_instock)
    RecyclerView rlInstock;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;

    TipsHelper mTipsHelper;
    private int mPageNum = 1;
    private TransferLogListAdapter mTransferLogListAdapter;
    private ArrayList<TransferLogBean.InfoBean> mListBeen = new ArrayList<>();

    public static void start(Context context) {
        Intent intent = new Intent(context, TransferActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_transfer;
    }

    @Override
    protected void initView() {
        getTvTitle().setText("移库");
        ivScan.setVisibility(View.VISIBLE);

        mTipsHelper = createTipsHelper(superRefresh);
        //refreshView下拉刷新 上拉加载
        superRefresh.setListener(this);
        superRefresh.setHeader(new DefaultHeader(this, true));
        superRefresh.setFooter(new DefaultFooter(this, true));
        //mInviteAdapter recycleview适配器
        mTransferLogListAdapter = new TransferLogListAdapter(this, mListBeen);
        //设置为纵向
        rlInstock.setLayoutManager(new LinearLayoutManager(this));
        //必写
        rlInstock.setHasFixedSize(true);
        rlInstock.setAdapter(mTransferLogListAdapter);
    }

    @OnClick({R.id.iv_scan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                IntentIntegrator integrator = new IntentIntegrator(this);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
                integrator.setPrompt("扫描条形码");
                integrator.setCameraId(0);
                integrator.setOrientationLocked(false);
                integrator.setBeepEnabled(true);//扫描提示音
                integrator.initiateScan();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null)  {
                ToastUtils.showShort("扫码取消！");
            } else {
                PalletGoodsActivity.start(this, result.getContents(), "");
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        mTipsHelper.hideLoading();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideEmpty();
        mTipsHelper.hideError();
        superRefresh.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {
        mPageNum++;
        getTransLogData();
    }

    @Override
    public void getDate() {
        mListBeen.clear();
        mPageNum = 1;
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideError();
        mTipsHelper.showLoading(true);
        getTransLogData();
    }

    private void getTransLogData() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("pageSize", Constants.PAGE_SIZE + "");
        map.put("pageNo", mPageNum + "");
        mPresenter.getTransferLogList(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", Constants.PAGE_SIZE, mPageNum);
    }

    @Override
    public void getTransferLogList(TransferLogBean transferLogBean) {
        //请求成功
        if (ToolsUtils.isEmptyList(transferLogBean.getInfo()) && mPageNum > 1) {
            ToastUtils.showShort(getString(R.string.no_data));
            return;
        }
        mListBeen.addAll(transferLogBean.getInfo());
        mTransferLogListAdapter.notifyDataSetChanged();
        if (transferLogBean.getInfo().isEmpty()) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

}
